package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanFileQO;
import com.xingkeduo.pojo.query.CraftsmanHistoryQO;
import com.xingkeduo.pojo.query.CraftsmanQO;
import com.xingkeduo.pojo.table.CraftsmanFileTO;
import com.xingkeduo.pojo.table.CraftsmanHistoryTO;
import com.xingkeduo.pojo.table.CraftsmanTO;
import com.xingkeduo.pojo.table.CraftsmanTO.Status;
import com.xingkeduo.util.pojo.enums.HistoryTypeEnum;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一线员工封装类
 *
 * @author 高杰
 * @time 2018年12月25日 下午1:42:07
 * @description
 */
@Component
public class CraftsmanService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanTO.class);
    private static final String NAMESPACE_HISTORY = CommonUtil.formatNamespace(CraftsmanHistoryTO.class);
    private static final String NAMESPACE_FILE = CommonUtil.formatNamespace(CraftsmanFileTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanTO, CraftsmanQO> dao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanHistoryTO, CraftsmanHistoryQO> historyDao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanFileTO, CraftsmanFileQO> fileDao;

    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 下午6:26:11
     */
//    public Optional<CraftsmanTO> selectByPrimary(long id) throws Throwable {
//        return Optional.ofNullable(dao.select(NAMESPACE, id));
//    }
    public CraftsmanTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午3:49:16
     */
    public CraftsmanTO selectByPrimaryWithHistory(long id) throws Throwable {
        CraftsmanTO craftsmanTable = dao.select(NAMESPACE, id);
        craftsmanTable.setHistorys(historyDao.select(NAMESPACE_HISTORY, new CraftsmanHistoryQO().setId(id)));
        return craftsmanTable;
    }

    /**
     * 查询
     *
     * @param code
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 下午6:26:11
     */
    public CraftsmanTO selectByLogin(String login) throws Throwable {
        CraftsmanQO craftsmanQuery = new CraftsmanQO().setLogin(login);
        List<CraftsmanTO> list = dao.select(NAMESPACE, craftsmanQuery);
        if (list == null || list.size() < 1)
            return null;
        if (list.size() > 1)
            throw new DataException(DataException.Type.NOTONLY, "发型师");
        return list.get(0);
    }

    /**
     * @param code:
     * @return : com.xingkeduo.pojo.table.CraftsmanTO
     * @author : gaozifeng
     * @date : 2019/5/18 13:35
     */
    public CraftsmanTO selectByCode(long code) throws Throwable {
        CraftsmanQO craftsmanQuery = new CraftsmanQO().setCode(code);
        List<CraftsmanTO> list = dao.select(NAMESPACE, craftsmanQuery);
        if (list == null || list.size() < 1)
            return null;
        if (list.size() > 1)
            throw new DataException(DataException.Type.NOTONLY, "发型师工号不唯一");
        return list.get(0);
    }


    /**
     * 查询
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午2:22:21
     */
    public List<CraftsmanTO> selectByQuery(CraftsmanQO query) throws Throwable {
        query.setIsDelete(YesNoEnum.NO);
        return dao.select(NAMESPACE, query);
    }

    /**
     * 查询id集合
     *
     * @param query:
     * @return : java.util.List<java.lang.Long>
     * @author : gaozifeng
     * @date : 2019/7/5 16:35
     */
    public List<Long> selectById(CraftsmanQO query) throws Throwable {
        return dao.selectIndex(NAMESPACE, query);
    }

    /**
     * 查询总数
     *
     * @param query
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月9日 下午5:06:44
     */
    public int count(CraftsmanQO query) throws Throwable {
        return dao.selectCount(NAMESPACE, query);
    }

    /**
     * 添加
     *
     * @param craftsman
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:13:21
     */
    @Transactional
    public void insert(CraftsmanTO craftsman) throws Throwable {
        craftsman.setStatus(Status.STUDENT);
        craftsman.setIsDelete(YesNoEnum.NO);
        if (dao.insert(NAMESPACE, craftsman) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工账户信息");

        CraftsmanHistoryTO history = new CraftsmanHistoryTO().setHistoryId(IdentityUtil.nextId(Worker.USER))
                .setCraftsman(craftsman.setIsDelete(YesNoEnum.NO).setVersion(1)).setHistoryType(HistoryTypeEnum.NEW)
                .setCreatedBy(craftsman.getCreatedBy()).setUpdatedBy(craftsman.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);
    }

    /**
     * 修改
     *
     * @param craftsman
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月27日 上午11:37:28
     */
    @Transactional
    public void update(CraftsmanTO craftsman) throws Throwable {
        if (dao.update(NAMESPACE, craftsman) < 1)
            throw new DataException(DataException.Type.FAIL, "一线员工账户信息");
        if (null != craftsman.getFiles()) {
            for (int i = 0; i < craftsman.getFiles().size(); i++) {
                craftsman.getFiles().get(i).setId(IdentityUtil.nextId(Worker.USER));
            }
            if (fileDao.insert(NAMESPACE_FILE, craftsman.getFiles()) < craftsman.getFiles().size())
                throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
        craftsman = dao.select(NAMESPACE, craftsman.getId());

        CraftsmanHistoryTO history = new CraftsmanHistoryTO().setHistoryId(IdentityUtil.nextId(Worker.USER))
                .setCraftsman(craftsman).setHistoryType(HistoryTypeEnum.MODIFY).setCreatedBy(craftsman.getUpdatedBy())
                .setUpdatedBy(craftsman.getUpdatedBy());
        historyDao.insert(NAMESPACE_HISTORY, history);

    }
}
